const fp = require('loadsh/fp')
//函子
class Containner {
	static of(value) {
		return new Containner(value)
	}
	constructor(value) {
		this._value = value
	}
	map(fn) {
		return Containner.of(fn(this._value))
	}
}
class Maybe {
	static of(value) {
		return new Maybe(value)
	}
	isNothing() {
		return this._value === null || this._value === undefined
	}
	constructor(value) {
		this._value = value
	}

	map(fn) {
		console.log(this.isNothing())
		return this.isNothing() ? this : Maybe.of(fn(this._value))
	}
}

//1 使用fp.add(x.y) 和 fp.map(f,x) 创建 一个能让functor 里的值增加的函数ex1
let maybe = Maybe.of([5, 6, 1])
let ex1 = () => {
	let w = maybe.map(fp.map((v) => fp.add(v, 3)))
	console.log(w)
}
ex1()
//2 fp.first 获取列表的第一个元素
let xs = Containner.of(['do', 'ray', 'me'])
let ex2 = () => {
	let x = xs.map(fp.first)
	console.log(x)
}
ex2()
//3 safeProp fp.first 找到user 的名字的首字母
let safeProp = fp.curry((x, o) => Maybe.of(o[x]))
let user = {
	id: 2,
	name: 'Albert',
}
let ex3 = () => {
	let u = safeProp('name')(user)
	u = u.map(fp.first)._value
	console.log(u)
}
ex3()
//4
let ex4 = (n) => {
	return Maybe.of(n).map(parseInt)
}
console.log(ex4(4.44))
console.log(ex4(null))
